﻿using System;
using System.Collections.Generic;
using System.Linq;
using ProjetoComperio.DataLayer.Entities;
using ProjetoComperio.DataLayer.Infra;
using ProjetoComperio.Framework.Extensions;

namespace ProjetoComperio.BusinessLogicLayer
{
    public class PersonBLL : ComperioDataObject
    {
        #region EducationLevel

        public List<EducationLevel> RetrieveEducationLevels()
        {
            return DbContext.EducationLevels.ToList();
        }

        public EducationLevel RetrieveEducationLevelByID(Int32 educationLevelID)
        {
            return RetrieveEducationLevels().Where(educationLevel => educationLevel.EducationLevelID == educationLevelID).FirstOrDefault();

        }

        #endregion

        public void Create(Person entity)
        {
            if (String.IsNullOrEmpty(entity.WebPage))
                entity.WebPage = "http://www.faetec.rj.gov.br/ist-rio";
            DbContext.People.Add(entity);
            DbContext.SaveChanges();

        }

        public void Edit(Person entity)
        {
            Person original = this.RetrievePerson(entity.PersonID);
            original.CopyValuesFrom(entity);
            DbContext.SaveChanges();

        }

        public Person RetrievePerson(Int32 entityID)
        {
            return DbContext.People.Where(entity => entity.PersonID == entityID).FirstOrDefault();
        }

        public IQueryable<Person> RetrievePeople()
        {
            return this.DbContext.People.AsQueryable();
        }

        public void Delete(Person entity)
        {

            DbContext.People.Remove(entity);
            DbContext.SaveChanges();

        }

        public List<Person> RetrievePeopleWhoWantToReceiveComperioNews()
        {
            return DbContext.People.Where(p => p.ReceiveComperioNews == true).ToList();
        }
    }
}